﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;

namespace FYPHP
{
    public partial class SearchResult : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = -1;

            if (Session["searchText"].ToString()!=null)
            {
                try
                {
                    SqlCommand cmdProduct;
                    SqlDataReader dtrProduct;
                    conn.Open();

                    cmdProduct = new SqlCommand("SELECT DISTINCT * FROM Products p, Users u WHERE p.u_id=u.u_id AND (p_name LIKE @search OR p_desc LIKE @search)", conn);
                    cmdProduct.Parameters.AddWithValue("@search", "%" + Session["searchText"].ToString() + "%");
                    int num = Convert.ToInt32(cmdProduct.ExecuteScalar());

                    if (num != 0)
                    {
                        dtrProduct = cmdProduct.ExecuteReader();

                        ItemList.DataSource = dtrProduct;
                        ItemList.DataBind();
                        dtrProduct.Close();

                        MultiView1.ActiveViewIndex = 0;
                    }
                    else
                    {
                        MultiView1.ActiveViewIndex = 1;
                    }

                    conn.Close();

                }
                catch (SqlException ex)
                {
                    MessageBox(ex.Message);
                }
            }
            else
            {
                MultiView1.ActiveViewIndex = 1;
            }
        }

        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }
    }
}